﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySaveZone : EnemyBase
{
    public int hitCount;
    public int hitMaxCount;
    float saveZoneDuration = 1f;

    float rawPosX;
    float attackPosX;

    float lastActionTimeStamp;

    public SafeZoneStatus status = SafeZoneStatus.SAFE_ZONE;

    public static float safeZoneMin = 5f;
    public static float safeZoneMax = 8f;

    public override void Init(EnemyMode mode, Vector3 spawnPos, FaceDir dir)
    {
        base.Init(mode, spawnPos, dir);

        hitMaxCount = Random.Range(3, 6);
        rawPosX = spawnPos.x;

        if (dir == FaceDir.FACE_DIR_LEFT)
        {
            attackPosX = Random.Range(safeZoneMin, safeZoneMax);
        }
        else if (dir == FaceDir.FACE_DIR_RIGHT)
        {
            attackPosX = Random.Range(-safeZoneMax, -safeZoneMin);
        }
        lastActionTimeStamp = Time.time;
    }

    protected override void FixedTick(float fixedDeltaTime)
    {
        if (bIsDead)
        {
            return;
        }

        float offsetY = Mathf.Sin(Time.time * VerticalOffsetFrequency) * VerticalOffsetRange;
        float x = rb2d.position.x;
        float y = rb2d.position.y + offsetY;

        if (status == SafeZoneStatus.SAFE_ZONE)
        {
            if (Time.time >= lastActionTimeStamp + saveZoneDuration)
            {
                lastActionTimeStamp = Time.time;
                status = SafeZoneStatus.SAFE_TO_ATTACK;
            }
        }
        else if (status == SafeZoneStatus.SAFE_TO_ATTACK)
        {
            if (Mathf.Abs(rb2d.position.x - attackPosX) < 0.1f)
            {
                lastActionTimeStamp = Time.time;
                hitCount = 0;
                bCanAttack = true;
                hitMaxCount = Random.Range(3, 6);
                status = SafeZoneStatus.ATTACK_ZONE;
            }
            else
            {
                //去attackzone就是前进
                x = Mathf.Clamp(x + HorizontalSpeed * fixedDeltaTime, Mathf.Min(x, attackPosX), Mathf.Max(x, attackPosX));
            }
        }
        else if (status == SafeZoneStatus.ATTACK_ZONE)
        {
            if (hitCount >= hitMaxCount)
            {
                lastActionTimeStamp = Time.time;
                bCanAttack = false;
                status = SafeZoneStatus.ATTACK_TO_SAFE;
            }
        }
        else if (status == SafeZoneStatus.ATTACK_TO_SAFE)
        {
            if (Mathf.Abs(rb2d.position.x - rawPosX) < 0.1f)
            {
                lastActionTimeStamp = Time.time;
                status = SafeZoneStatus.SAFE_ZONE;
            }
            else
            {
                //回savezone就是退后
                x = Mathf.Clamp(x - HorizontalSpeed * fixedDeltaTime, Mathf.Min(x, rawPosX), Mathf.Max(x, rawPosX));
            }
        }

        rb2d.MovePosition(new Vector2(x, y));
    }

    protected override void ExecAttack()
    {
        base.ExecAttack();
        hitCount++;
    }

}